/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Brand;
import java.sql.*;import java.util.Vector;
;
/**
 *
 * @author mishra
 */
public class Query {
    static Connection con=null;
    public static int  add(BrandBean bb)throws SQLException{
        int i=0;
        try{
            con=DBConnection.Connection.getConnection();
            PreparedStatement pre=con.prepareStatement("insert into brand values(BRANDID.nextval,?,sysdate)");
            pre.setString(1, bb.getBrandName());
            i=pre.executeUpdate();
        }catch(Exception e){
            System.out.println(e);
        }finally{
            con.close();
        }return i;
    }
    public static int edit(BrandBean bb)throws SQLException{
        int i=0;
        try{
            con=DBConnection.Connection.getConnection();
            PreparedStatement pre=con.prepareStatement("update brand set brand_name=?, entry_date=sysdate where brand_id=?");
            pre.setString(1, bb.getBrandName());
            pre.setInt(2, bb.getBrandId());
            i=pre.executeUpdate();
        }catch(Exception e){
            System.out.println(e);
        }finally{
            con.close();
        }return i;
    }
    public static Vector viewAll()throws SQLException{
        Vector<Object> list=new Vector<Object>();
        try{
            con=DBConnection.Connection.getConnection();
            PreparedStatement pre=con.prepareStatement("select * from brand order by brand_name");
            ResultSet rs=pre.executeQuery();
            while(rs.next()){
                BrandBean bb=new BrandBean();
                bb.setBrandId(rs.getInt(1));
                bb.setBrandName(rs.getString(2));
                bb.setEntryDate(rs.getDate(3));
                list.add(bb);
            }
        }catch(Exception e){
            System.out.println(e);
        }finally{
            con.close();
        }return list;
    }
    
    public static BrandBean viewSpecific(String cname, String val)throws SQLException{
         BrandBean bb=new BrandBean();
        try{
            con=DBConnection.Connection.getConnection();
            PreparedStatement pre=con.prepareStatement("select * from brand where "+cname+"="+val+"");
            ResultSet rs=pre.executeQuery();
            while(rs.next()){
               
                bb.setBrandId(rs.getInt(1));
                bb.setBrandName(rs.getString(2));
                bb.setEntryDate(rs.getDate(3));
            }
        }catch(Exception e){
            System.out.println(e);
        }finally{
            con.close();
        }return bb;
    }
    public static int delete(int id)throws SQLException{
        int i=0;
        try{
            con=DBConnection.Connection.getConnection();
            PreparedStatement pre=con.prepareStatement("delete from brand where brand_id=?");
            pre.setInt(1, id);
            i=pre.executeUpdate();
        }catch(Exception e){
            System.out.println(e);
        }finally{
            con.close();
        }return i;
    }
}
